package com.kla.power.controller;

import com.kla.power.config.LogPointer;
import com.kla.power.entity.AlarmEntity;
import com.kla.power.entity.DeviceEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IAlarmService;
import com.kla.power.service.IDeviceService;
import com.kla.power.util.ConstUtil;
import com.kla.power.util.RedisUtils;
import com.kla.power.util.ResultBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警/故障 前端控制器
 * </p>
 * @author WuZH
 * @since 2023-06-23
 */
@RestController
@RequestMapping ("/alarm")
@CrossOrigin (value = "*", maxAge = 3600)
public class AlarmController {

    @Resource
    private IAlarmService iAlarmService;
    @Resource
    private IDeviceService iDeviceService;

    @RequestMapping ("queryList")
    @LogPointer ("警告列表")
    public ResultBody queryList (@RequestBody AlarmEntity alarmEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( alarmEntity.getToken () );
        // 查询该商户下的所有报警
        alarmEntity.setMerchantId ( userEntity.getMerchantId () );
        alarmEntity.setType ( ConstUtil.DEVICE_TYPE_INVERTER );
        List<AlarmEntity> alarmEntities = iAlarmService.queryList ( alarmEntity );
        return ResultBody.success ( alarmEntities, iAlarmService.queryCount ( alarmEntity ) );
    }

    /**
     * 报警详情页
     * @param alarmEntity
     * @return
     */
    @RequestMapping ("querySingle")
    @LogPointer ("警告详情页")
    public ResultBody querySingle (@RequestBody AlarmEntity alarmEntity) {
        alarmEntity = iAlarmService.querySingle ( alarmEntity );
        return ResultBody.success ( alarmEntity );
    }


    /**
     * 通过电站Id查询所有报警，用于新增工单时
     * @param alarmEntity
     * @return
     */
    @RequestMapping ("alarmList")
    @LogPointer ("查询一个电站下警告列表")
    public ResultBody alarmList (@RequestBody AlarmEntity alarmEntity) {
        List<AlarmEntity> alarmEntities = iAlarmService.queryAlarmByStation ( alarmEntity );
        return ResultBody.success ( alarmEntities );
    }

    /**
     * 告警详情页告警折线图，查询当出现警告的时候，对应逆变器的电流和电压
     * @param alarmEntity
     * @return
     */
    @RequestMapping ("alarmHistoryData")
    public ResultBody alarmHistoryData (@RequestBody AlarmEntity alarmEntity) {
        AlarmEntity alarm = iAlarmService.querySingle ( alarmEntity );
        DeviceEntity deviceEntity = new DeviceEntity ();
        deviceEntity.setTimeType ( 1 );
        // 发生告警的那一天的日期
        deviceEntity.setStartTime ( alarm.getCreateTime () );
        deviceEntity.setEndTime ( alarm.getCreateTime () );
        deviceEntity.setDeviceSn ( alarm.getDeviceSn () );
        Map<Object, Object> map = iDeviceService.queryDcHistoryData ( deviceEntity );
        return ResultBody.success ( map );
    }

}
